﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

namespace LightPersist
{
	internal class DagCheckingContext
	{
		readonly Hashtable _hashtableOfCurrentStack = new Hashtable();
		readonly Stack<Guid> _currentStack = new Stack<Guid>();

		public void Push(Guid id)
		{
			_hashtableOfCurrentStack.Add(id, null);
			_currentStack.Push(id);

			Check();
		}

		public void Pop()
		{
			var id = _currentStack.Pop();
			_hashtableOfCurrentStack.Remove(id);

			Check();
		}

		[Conditional("DEBUG")]
		void Check()
		{
			Debug.Assert(_currentStack.Count == _hashtableOfCurrentStack.Count);
		}

		public void EnshureStackIsEmpty()
		{
			if (_currentStack.Count > 0)
			{
				throw new ApplicationException("The engine of cycle detection is in invalid state");
			}

			Check();
		}
	}
}
